package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.DicValue;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.DicValueService;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Clue;
import com.bjpowernode.crm.workbench.domain.ClueRemark;
import com.bjpowernode.crm.workbench.service.ClueRemarkService;
import com.bjpowernode.crm.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ClueController {

    @Autowired
    private UserService userService;

    @Autowired
    private DicValueService dicValueService;

    @Autowired
    private ClueService clueService;

    @Autowired
    private ClueRemarkService clueRemarkService;

    @RequestMapping(value = "/workbench/clue/index.do")
    public String index(HttpServletRequest request){
        try {
            List<User> userList = userService.queryAllUsers();
            List<DicValue> appellationList = dicValueService.queryDicValueByTypeCode("appellation");
            List<DicValue> clueStateList = dicValueService.queryDicValueByTypeCode("clueState");
            List<DicValue> sourceList = dicValueService.queryDicValueByTypeCode("source");

            request.setAttribute("userList",userList);
            request.setAttribute("appellationList",appellationList);
            request.setAttribute("clueStateList",clueStateList);
            request.setAttribute("sourceList",sourceList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "workbench/clue/index";
    }

    @RequestMapping(value = "/workbench/clue/saveCreateClue.do")
    @ResponseBody
    public Object saveCreateClue(Clue clue, HttpSession session){
        ReturnObject returnObject = new ReturnObject();
        User user = (User)session.getAttribute(Contants.SESSION_USER);

        clue.setId(UUIDUtils.getUUID());
        clue.setCreateBy(user.getId());
        clue.setCreateTime(DateUtils.formatDataTime(new Date()));

        try {
            int res = clueService.saveCreateClue(clue);
            if(res > 0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            }else{
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙,请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙,请稍后再试");
        }

        return returnObject;
    }

    @RequestMapping(value = "/workbench/clue/queryClueByConditionForPage.do")
    @ResponseBody
    public Object queryClueByConditionForPage(String fullName,String company,String phone,String source,
            String owner,String mphone,String state,int pageNo,int pageSize){
        Map map = new HashMap();
        map.put("fullName",fullName);
        map.put("company",company);
        map.put("phone",phone);
        map.put("source",source);
        map.put("owner",owner);
        map.put("mphone",mphone);
        map.put("state",state);
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);

        List<Clue> clueList = clueService.queryClueByConditionForPage(map);
        int totalRows = clueService.queryCountOfClueByConditionForPage(map);

        Map<String,Object> resultMap = new HashMap<>();

        resultMap.put("clueList",clueList);
        resultMap.put("totalRows",totalRows);

        return resultMap;
    }

    @RequestMapping(value = "/workbench/clue/deleteClueByIds.do")
    @ResponseBody
    public Object deleteClueByIds(String[] ids){
        ReturnObject returnObject = new ReturnObject();

        try {
            int res = clueService.deleteClueByIds(ids);
            if(res > 0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            }else{
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙,请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙,请稍后再试");
        }

        return returnObject;
    }

    @RequestMapping(value = "/workbench/clue/queryClueById.do")
    @ResponseBody
    public Object queryClueById(String id){
        ReturnObject returnObject = new ReturnObject();

        try {
            Clue clue = clueService.queryClueById(id);
            if(clue != null){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setRetData(clue);
            }else{
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙,请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙,请稍后再试");
        }

        return returnObject;
    }

    @RequestMapping(value = "/workbench/clue/updateClueById.do")
    @ResponseBody
    public Object updateClueById(Clue clue,HttpSession session){
        User user = (User)session.getAttribute(Contants.SESSION_USER);

        clue.setEditBy(user.getId());
        clue.setEditTime(DateUtils.formatDataTime(new Date()));

        ReturnObject returnObject = new ReturnObject();

        try {
            int res = clueService.updateClueById(clue);
            if(res > 0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            }else{
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙,请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙,请稍后再试");
        }

        return returnObject;
    }

    @RequestMapping(value = "/workbench/clue/detailActivity.do")
    public String detailActivity(String id,HttpServletRequest request){

        try {
            Clue clue = clueService.queryClueByIdForDetail(id);
            List<ClueRemark> clueRemarkList = clueRemarkService.queryAllClueRemarkByClueId(id);
            request.setAttribute("clue",clue);
            request.setAttribute("clueRemarkList",clueRemarkList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "workbench/clue/detail";
    }
}
